package form.ui.core {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Toggle extends Container {
		private var _on:DisplayObject;
		private var _off:DisplayObject;
		private var _disabled:DisplayObject;
		
		private var _isOn:Boolean = true;
		
		private var _clickable:Boolean = false;
		
		protected var enabled:Boolean = false;
		
		public function Toggle() {
			super();
			
			this.mouseChildren = false;
			this.clickable = true;
			
			this.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
			
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);			
			
		}
		
		public function onAction():void {	
		}
		
		public function offAction():void {	
		}
		
		public function enable():void {
			if (!this.enabled) {
				this.enabled = true;
				if (this.isOn) { this.showOn(); }
				else { this.showOff(); }
			}
		}
		
		public function disable():void {
			if (this.enabled) {
				this.enabled = false;
				this.showDisabled();
			}
		}
		
		public function set on(object:DisplayObject):void {
			if (this.on != null) {
				this.removeChild(this.on);
			}
			
			this._on = object;
			this.addChild(this.on);
		}
		
		public function get on():DisplayObject {
			return this._on;
		}
		
		public function set off(object:DisplayObject):void {
			if (this.off != null) {
				this.removeChild(this.off);
			}
			
			this._off = object;
			this.addChild(this.off);
		}
		
		public function get off():DisplayObject {
			return this._off;
		}
		
		public function set disabled(object:DisplayObject):void {
			if (this.disabled != null) {
				this.removeChild(this.disabled);
			}
			
			this._disabled = object;
			this.addChild(this.disabled);
		}
		
		public function get disabled():DisplayObject {
			return this._disabled;
		}
		
		public function get isOn():Boolean {
			return this._isOn;
		}
		
		public function turnOn():void {
			this._isOn = true;
			this.showOn();
			this.onAction();
		}
		
		public function turnOff():void {
			this._isOn = false;
			this.showOff();
			this.offAction();
		}
		
		protected function showOn():void {
			this.clickable = true;
			
			if (this.on != null) { this.on.visible = true; }
			if (this.off != null) { this.off.visible = false; }
			if (this.disabled != null) { this.disabled.visible = false; }
		}
		
		protected function showOff():void {
			this.clickable = true;
			
			if (this.on != null) { this.on.visible = false; }
			if (this.off != null) { this.off.visible = true; }
			if (this.disabled != null) { this.disabled.visible = false; }
		}
		
		protected function showDisabled():void {
			this.clickable = false;
			
			if (this.on != null) { this.on.visible = false; }
			if (this.off != null) { this.off.visible = false; }
			if (this.disabled != null) { this.disabled.visible = true; }
		}
		
		protected function onMouseUp(e:Event):void {
			if (this.isOn) {
				this.turnOff();
			} else {
				this.turnOn();
			}
		}
		
		protected function onAddedToStage(e:Event):void {
			this.enable();
		}
		
		protected function set clickable(value:Boolean):void {
			this._clickable = value;
			
			this.mouseEnabled = this.clickable;
			this.buttonMode = this.clickable;
			this.useHandCursor = this.clickable;	
		}
		
		protected function get clickable():Boolean {
			return this._clickable;
		}
	
	}
}